<?php
namespace App\Models;
use Illuminate\Http\Request;

/**
 * 会员属性
 */
class MUserAttr extends BaseModel
{
    public $table = "user_attr";

    public static $USER_ATTR_OFF = 0;//会员属性关闭
    public static $USER_ATTR_ON = 1;//会员属性开启

    public static $REG_ATTR_OFF = 0;//会员属性注册权限关
    public static $REG_ATTR_ON = 1;//会员属性注册权限开

    public static $WITHDRAW_ATTR_OFF = 0;//会员属性提现权限关
    public static $WITHDRAW_ATTR_ON = 0;//会员属性提现权限开

    public static $TRANSFER_ATTR_OFF = 0;//会员属性转账权限关
    public static $TRANSFER_ATTR_ON = 1;//会员属性转账权限开

    public static $ADDR_ATTR_OFF = 0;//会员属性收货地址权限关
    public static $ADDR_ATTR_ON = 1;//会员属性收货地址权限开

    public static $REGISTER_ATTR = 'reg';//会员属性中的注册权限
    public static $WITHDRAW_ATTR = 'withdraw';//会员属性中的提现权限
    public static $TRANSFER_ATTR = 'transfer';//会员属性中的转账权限
    public static $ACCEPT_ADDR_ATTR = 'accept_addr';//会员属性中的收货地址权限
    /**
     * 会员属性编辑数据验证规则
     * $flag 0 = rules  1 = msg
     */
    public static function editInfoRulesWithMsg ($flag)
    {
        $rules = array(
            'name' => "required|between:2,20",
        );

        $msg = array(
            "name.required" => "名称不能为空",
            "name.between"  => "名称只能位于2-20位之间",
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    /** 检查标示是否唯一
     * @param $mark 检测的标示
     * @return bool true 标示已经存在  false 标示不存在
     */
    public function exitMark ($mark)
    {
        if (MUserAttr::where("mark",$mark)->count() != 0) {
            return true;
        } else {
            return false;
        }

    }

    /** 检查名称是否存在
     * @param $name 名称
     * @return bool true 存在 false 不存在
     */
    public function exitName ($name)
    {
        if (MUserAttr::where("name",$name)->count() != 0) {
            return true;
        } else {
            return false;
        }

    }

    public function exitNameExcept ($name,$id)
    {
        if (MUserAttr::where("name",$name)->where('id','!=',$id)->count() != 0) {
            return true;
        } else {
            return false;
        }
    }

    /** 添加会员属性
     * @param Request $request 属性数据
     * @return bool true 添加成功 false 添加失败
     */
    public function attrSave (Request $request,$mark)
    {
        $reg = isset($request->reg)?$request->reg : self::$REG_ATTR_OFF;//注册权限
        $withdraw = isset($request->withdraw)?$request->withdraw : self::$WITHDRAW_ATTR_OFF;//提现权限
        $transfer = isset($request->transfer)?$request->transfer : self::$TRANSFER_ATTR_OFF;//转账权限
        $accept_addr = isset($request->accept_addr)?$request->accept_addr : self::$ADDR_ATTR_OFF;//收货地址权限

        $flag_arr = array();//属性权限
        $flag_arr['reg'] = $reg;
        $flag_arr['withdraw'] = $withdraw;
        $flag_arr['transfer'] = $transfer;
        $flag_arr['accept_addr'] = $accept_addr;
        $flag = serialize($flag_arr);

        $rules_arr = array(
           $mark => "required|alpha_dash|between:3,50",
        );
        $rules = serialize($rules_arr);

        $msg_arr = array(
            $mark.".required" => $request->name."不能为空",
            $mark.".alpha_dash" => $request->name."字符仅包含字母 数字 破折号(-) 下划线(_)",
            $mark.".between"  => $request->name."字符只能3-50位之间",

        );
        $msg = serialize($msg_arr);

        $this->name = $request->name;
        $this->mark = $mark;
        $this->flag = $flag;
        $this->rule = $rules;
        $this->msg = $msg;
        $this->enable = self::$USER_ATTR_ON;
        return $this->save();
    }

    /** 反序列化数组
     * @param $serialize 序列化数组
     * @return mixed  数组
     */
    public static function getUnArray ($serialize)
    {
         $val =  unserialize($serialize);
         return $val;
    }

    /** 关闭会员属性信息状态
     * @return bool true 关闭 false 关闭失败
     */
    public function editAttrSave ()
    {
        $this->enable = self::$USER_ATTR_OFF;
        return $this->save();
    }

    /** 开启会员属性
     * @return bool true 成功 false 不成功
     */
    public function onAttrSave ()
    {
        $this->enable = self::$USER_ATTR_ON;
        return $this->save();
    }

    /** 通过前台传自定义属性 向user_info中添加属性数据
     * @param $uid 用户
     * @param $request 传过来的值
     * @return bool true 添加成功 false 添加失败
     */
    public static function userInfo ($uid, $request)
    {
        foreach ($request as $k=>$v) {
            if (starts_with($k,'attr_')) {
                if (!MUserAttr::saveUserInfo($uid,$k,$v)) {
                    return false;
                }
            }
        }
        return true;
    }

    /** 向user_info表中添加数据
     * @param $uid 用户id
     * @param $k 标识
     * @param $v 值
     * @return bool true保存成功 false保存失败
     */
    public static function saveUserInfo ($uid, $k, $v)
    {
        $info = new MUserInfo();
        $info->uid = $uid;
        $info->mark = $k;
        $info->value = $v;
        return $info->save();
    }

    /** 執行屬性是刪除
     * @return bool
     * @throws \Exception
     */
    public function UserInfoDel ()
    {
        if (self::exitUserInfoMark($this->mark)) {
            $infoDel = MUserInfo::where("mark",$this->mark)->delete();
        } else {
            $infoDel = true;
        }
        return $this->delete() && $infoDel;
    }

    /** 检查标识是否存在
     * @param $mark 名称
     * @return bool true 存在 false 不存在
     */
    public static function exitUserInfoMark ($mark)
    {
        if (MUserInfo::where("mark",$mark)->count() != 0) {
            return true;
        } else {
            return false;
        }
    }
}